﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Baidu.Aip.Face;

namespace Ftest
{
    public partial class Form1 : Form
    {
        //static readonly string gsAppId = "10584005";
        //static readonly string gsApiKey = "ckhbGEGXid9h39ZrGbfhCtMH";
        //static readonly string gsSecretKey = "dGhTsHhY1eEw33w60C7KI8LAvW420jKC";

        Face client = new Face(gpBaidu.gsApiKey, gpBaidu.gsSecretKey);
        public Form1()
        {
            InitializeComponent();
        }

        private void Msg(object _msg)
        {
            Msg(_msg,true);
        }
        private void Msg(object _msg,bool clearContent)
        {
            if (clearContent)
                tbMsg.Text = _msg.ToString();
            else
                tbMsg.Text += "\r\n" + _msg.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!File.Exists(textBox1.Text))
            {
                Msg("未选择图像文件。");
                return;
            }
            var image = File.ReadAllBytes(textBox1.Text);
            var options = new Dictionary<string, object>
            {
                {"max_face_num", 2},
                {"face_fields", "beauty,age,qualities"}                
            };
            var result = client.FaceDetect(image, options);
            Msg(result);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;
            textBox1.Text = dlg.FileName;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var result = client.Group.GetAllGroups(0, 100);
            Msg(result);
            mod _mod = Newtonsoft.Json.JsonConvert.DeserializeObject<mod>(result.ToString());
            for (int i = 0; i < _mod.result.Length; i++)
            {
                Msg(_mod.result[i]+"======================", false);
                var rR2 = client.Group.GetUsers(_mod.result[i]);
                Msg(rR2, false);
            }
            
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (!File.Exists(textBox1.Text))
            {
                Msg("未选择图像文件。");
                return;
            }
            var uid = "wgc";
            var userInfo = "wgchao";
            var groupId = "test,me".Split(',');
            var image = File.ReadAllBytes(textBox1.Text);
            //可能会抛出网络等异常，请使用try/catch捕获
            var result = client.User.Register(image, uid, userInfo, groupId);
            Msg(result);
            //// 如果有可选参数
            //var options = new Dictionary<string, object>{{"action_type", "replace"} };
            //result = client.User.Register(image, uid, userInfo, groupId, options);
            //Msg(result);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (!File.Exists(textBox1.Text))
            {
                Msg("未选择图像文件。");
                return;
            }
            var image1 = File.ReadAllBytes(textBox1.Text);
            var result = client.User.Verify(image1, "wgc", new[] { "test" }, 1);
            Msg(result);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (!File.Exists(textBox1.Text))
            {
                Msg("未选择图像文件。");
                return;
            }
            var cImageCensor = new Baidu.Aip.ImageCensor.AntiPorn(gpBaidu.gsApiKey, gpBaidu.gsSecretKey);
            var image = File.ReadAllBytes(textBox1.Text);
            // 过程中发生的网络失败等系统错误，将会抛出相关异常，请使用 try/catch 捕获。
            var result = cImageCensor.Detect(image);
            Msg(result);
        }
    }
}
